//
//  YJ_StatusFrame.m
//  YouJoin
//
//  Created by MacBookPro on 15/11/10.
//  Copyright © 2015年 MacBookPro. All rights reserved.
//
#define YJ_NameFont [UIFont systemFontOfSize:10]
#define YJ_TextFont [UIFont systemFontOfSize:12]

#import "YJ_StatusFrame.h"
#import "YJ_Status.h"

@implementation YJ_StatusFrame

/**
 *  计算文字尺寸
 *
 *  @param text    需要计算尺寸的文字
 *  @param font    文字的字体
 *  @param maxSize 文字的最大尺寸
 */
-(CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize{
    
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    
}

-(void)setStatus:(YJ_Status *)status{
    _status = status;
    
    CGFloat padding = 10;
    CGFloat commentHeight = 30;
    CGFloat cellPaddingHeight = 20;
    
    // 1.头像
    CGFloat iconX = padding;
    CGFloat iconY = padding;
    CGFloat iconW = 30;
    CGFloat iconH = 30;
    _incoPicF = CGRectMake(iconX, iconY, iconW, iconH);
    
    // 2.昵称
    // 文字的字体
    CGSize nameSize = [self sizeWithText:self.status.username font:YJ_NameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGFloat nameX = CGRectGetMaxX(_incoPicF) + padding;
    CGFloat nameY = iconY + (iconH - nameSize.height) * 0.5;
    _usernameF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
    
    // 3.时间
    CGSize timerSize = [self sizeWithText:self.status.timer font:YJ_NameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGFloat timerX = 320 - padding * 4;
    CGFloat timerY = nameY;
    _timerF = CGRectMake(timerX, timerY, timerSize.width, timerSize.height);
    
    // 4.正文
    CGFloat textX = iconX;
    CGFloat textY = CGRectGetMaxY(_incoPicF) + padding;
    CGSize textSize = [self sizeWithText:self.status.textContent font:YJ_TextFont maxSize:CGSizeMake(300, MAXFLOAT)];
    _textContentF = CGRectMake(textX, textY, textSize.width, textSize.height);
    
    // 5.配图
    if (self.status.picture) {// 有配图
        CGFloat pictureX = textX;
        CGFloat pictureY = CGRectGetMaxY(_textContentF) + padding;
        CGFloat pictureW = 100;
        CGFloat pictureH = 100;
        _pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH);
        
        _cellHeight = CGRectGetMaxY(_pictureF) + cellPaddingHeight + commentHeight + padding;
    } else {
        _cellHeight = CGRectGetMaxY(_textContentF) + cellPaddingHeight + commentHeight + padding;
    }
    _commentF = CGRectMake(0, _cellHeight - cellPaddingHeight - commentHeight, 320, commentHeight);
    _cellPaddingF = CGRectMake(0, _cellHeight - cellPaddingHeight, 320, cellPaddingHeight);
}
@end
